<?php
require_once 'common.php';

// 获取当前页码，默认第1页
$currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$currentPage = max(1, $currentPage); // 确保页码不小于1

// 获取分类参数（如果有）
$category = isset($_GET['category']) ? $_GET['category'] : '';

// 调用API获取文章列表
$params = [
    'page' => $currentPage,
    'app_id' => 10001,
    'list_rows' => PAGE_SIZE
];
if (!empty($category)) {
    $params['category'] = $category;
}
$articleData = api_request(API_ARTICLE_LIST, $params);
$articles = isset($articleData['data']['list']) ? $articleData['data']['list']['data'] : [];

// 计算总页数
$total = isset($articleData['data']['list']['total']) ? $articleData['data']['list']['total'] : 0;
$totalPages = max(1, ceil($total / PAGE_SIZE));

// SEO标签配置
$page_title = "健康学堂 - " . SITE_NAME;
$page_desc = "提供专业的健康知识、养生技巧和生活建议，助您保持健康生活方式";
$page_keywords = "健康知识,养生技巧,健康生活," . SITE_NAME;
$canonical_url = build_url('article_list', ['category' => $category, 'page' => $currentPage]);
?>
<?php include 'templates/header.php'; ?>
<?php include 'templates/navbar.php'; ?>

<!-- 引入小红书风格样式 -->
<link rel="stylesheet" href="static/css/xiaohongshu.css">

	<!-- 页面标题 -->
	<section class="section page-header">
	  <div class="container mt30">
		<h1>健康学堂</h1>
		<p class="text-muted">
		  精选健康知识，呵护您的生活
		</p>
	  </div>
	</section>
<div class="container xhs-container">

    <!-- 瀑布流容器 -->
    <div class="xhs-masonry" id="xhs-feed">
        <?php if (!empty($articles)): ?>
            <?php foreach ($articles as $item): ?>
                <div class="xhs-card">
                    <a href="<?php echo build_url('article_detail', ['id' => $item['material_id']]); ?>">
                        <img src="<?php echo $item['image'] ?? 'https://picsum.photos/400/600'; ?>" 
                             alt="辰小膳 益生元 膳食纤维 低聚果糖  肠道健康  低聚半乳糖<?php echo htmlspecialchars($item['title'] ?? ''); ?>" 
                             class="xhs-card-img">
                        <div class="xhs-card-content">
                            <div class="xhs-card-title"><?php echo htmlspecialchars($item['title'] ?? ''); ?></div>
                            <div class="xhs-card-meta">
                                <div class="xhs-user">
                                    <img src="<?php echo $item['user']['avatarUrl'] ?? 'https://picsum.photos/100/100'; ?>" 
                                         alt="辰小膳 益生元 膳食纤维 低聚果糖  肠道健康  低聚半乳糖<?php echo htmlspecialchars($item['user']['nickName'] ?? '管理员'); ?>" 
                                         class="xhs-avatar">
                                    <span><?php echo htmlspecialchars($item['user']['nickName'] ?? '管理员'); ?></span>
                                </div>
                                <div class="xhs-like">
                                    <i class="far fa-heart"></i>
                                    <span><?php echo $item['likes'] ?? 0; ?></span>
                                </div>
                            </div>
                        </div>
                    </a>
                </div>
            <?php endforeach; ?>
        <?php else: ?>
            <div class="xhs-empty">
                <p>暂无相关文章</p>
            </div>
        <?php endif; ?>
    </div>

    <!-- 分页导航 -->
    <?php if ($totalPages > 1): ?>
    <div class="xhs-pagination mt-6 text-center mt30">
        <ul class="pagination justify-content-center">
            <!-- 上一页 -->
            <li class="page-item <?php echo ($currentPage == 1) ? 'disabled' : ''; ?>">
                <a class="page-link" href="<?php echo build_url('article_list', [
                    'category' => $category,
                    'page' => $currentPage - 1
                ]); ?>">上一页</a>
            </li>

            <!-- 页码按钮 -->
            <?php 
            // 显示当前页前后2页及首尾页
            $startPage = max(1, $currentPage - 2);
            $endPage = min($totalPages, $currentPage + 2);
            
            // 显示首页
            if ($startPage > 1) {
                echo '<li class="page-item"><a class="page-link" href="' . build_url('article_list', [
                    'category' => $category,
                    'page' => 1
                ]) . '">1</a></li>';
                if ($startPage > 2) echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
            }
            
            // 显示中间页码
            for ($i = $startPage; $i <= $endPage; $i++) {
                echo '<li class="page-item ' . ($i == $currentPage ? 'active' : '') . '">';
                echo '<a class="page-link" href="' . build_url('article_list', [
                    'category' => $category,
                    'page' => $i
                ]) . '">' . $i . '</a></li>';
            }
            
            // 显示尾页
            if ($endPage < $totalPages) {
                if ($endPage < $totalPages - 1) echo '<li class="page-item disabled"><span class="page-link">...</span></li>';
                echo '<li class="page-item"><a class="page-link" href="' . build_url('article_list', [
                    'category' => $category,
                    'page' => $totalPages
                ]) . '">' . $totalPages . '</a></li>';
            }
            ?>

            <!-- 下一页 -->
            <li class="page-item <?php echo ($currentPage == $totalPages) ? 'disabled' : ''; ?>">
                <a class="page-link" href="<?php echo build_url('article_list', [
                    'category' => $category,
                    'page' => $currentPage + 1
                ]); ?>">下一页</a>
            </li>
        </ul>
        <div class="pagination-info mt-2 text-muted">
            共 <?php echo $total; ?> 篇文章，当前第 <?php echo $currentPage; ?> / <?php echo $totalPages; ?> 页
        </div>
    </div>
    <?php endif; ?>
</div>

<?php include 'templates/footer.php'; ?>